/*
 * Copyright © 2024-2025 Benshou Technology Co., Ltd.
 * All rights reserved.
 */

package com.benshou.bsoa.common.trace;

import java.util.Base64;
import java.util.UUID;

public abstract class UUIDShort {
    public UUIDShort() {
    }

    public static String generate() {
        UUID uuid = UUID.randomUUID();
        return compressedUUID(uuid);
    }

    protected static String compressedUUID(UUID uuid) {
        byte[] byUuid = new byte[16];
        long least = uuid.getLeastSignificantBits();
        long most = uuid.getMostSignificantBits();
        long2bytes(most, byUuid, 0);
        long2bytes(least, byUuid, 8);
        return Base64.getUrlEncoder().withoutPadding().encodeToString(byUuid);
    }

    protected static void long2bytes(long value, byte[] bytes, int offset) {
        for(int i = 7; i > -1; --i) {
            bytes[offset++] = (byte)((int)(value >> 8 * i & 255L));
        }

    }

    public static void main(String[] args) {
        System.out.println(generate());
    }
}
